﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb
{
    public partial class MaintainData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // BEGDC.PreContract.BLL.IUserRole iuser = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];

                //                DataTable dt = iuser.GetRoleListByEmployeeId(((BEGDC.PreContract.BLL.Authentication.PCIdentity)Page.User.Identity).EmployeeId);

                // HttpContext.Current.User = BEGDC.PreContract.BLL.Authentication.PCPrincipal.GetPrincipal(HttpContext.Current.User);

                BEGDC.PreContract.BLL.Authentication.PCPrincipal principal = User as BEGDC.PreContract.BLL.Authentication.PCPrincipal;
                if (principal.IsInRole("Admin"))
                {
                    btnMaintain.Visible = true;
                    SetButton();
                }
                else
                {
                    btnMaintain.Visible = false;
                }
            }
        }

        private void SetButton()
        {
            bool maintainDataFlag = Global.MaintainStatus;
            if (maintainDataFlag)
            {
                btnMaintain.Text = "...数据维护中...";
                btnMaintain.ForeColor = System.Drawing.Color.Red;
                btnMaintain.ToolTip = "点击结束维护数据";
            }
            else
            {
                btnMaintain.Text = "数据维护";
                btnMaintain.ForeColor = System.Drawing.Color.Black;
                btnMaintain.ToolTip = "点击开始维护数据";
            }
        }

        protected void btnMaintain_Click(object sender, EventArgs e)
        {
            Global.MaintainStatus = !Global.MaintainStatus;
            SetButton();


            if (Global.MaintainStatus)
            {
                ShowTitleToMaintain("......数据维护中......");
            }
            else
            {
                ShowTitleToMaintain("");
            }
        }

        private void ShowTitleToMaintain(string title)
        {
            string js = @"<script>var pWin=window.parent;
                            if (pWin!=null)
                            {
                                var spnMaintainStatus=pWin.document.getElementById('spnMaintainStatus');
                                     spnMaintainStatus.innerText=  '" + title + @"';

                            }</script>";

            ClientScript.RegisterStartupScript(GetType(), "alert", js);
        }
    }
}
